package impl

import (
	"adam2/internal/domain"
	"adam2/internal/model"
	"anubis-framework/pkg/io"
)

type OptimizeAccountLogDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetOptimizeAccountLogDaoImpl() *OptimizeAccountLogDaoImpl {
	return &OptimizeAccountLogDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (o *OptimizeAccountLogDaoImpl) FindTableName() string {
	return "OPTIMIZE_ACCOUNT_LOG"
}

// 根据开始时间、结束时间查询记录，并按照date_和account_name列升序排列
func (o *OptimizeAccountLogDaoImpl) FindByBeginDateAndEndDateOrderByDateAndAccountName(beginDate string, endDate string) model.OptimizeAccountLogArray {
	io.Infoln("根据开始时间、结束时间查询记录，并按照date_和account_name列升序排列")

	// 查询记录
	var optimizeAccountLogArray *model.OptimizeAccountLogArray = &model.OptimizeAccountLogArray{}
	o.db.Raw("select * from optimize_account_log t "+
		"where t.date_ between to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd') "+
		"order by t.date_ asc, t.account_name asc", beginDate, endDate).Scan(optimizeAccountLogArray)

	return *optimizeAccountLogArray
}

// 根据开始时间、结束时间、account_name查询记录，并按照date_和account_name列升序排列
func (o *OptimizeAccountLogDaoImpl) FindByBeginDateAndEndDateAndAccountNameOrderByDateAndAccountName(beginDate string, endDate string, accountName string) model.OptimizeAccountLogArray {
	io.Infoln("根据开始时间、结束时间、account_name查询记录，并按照date_和account_name列升序排列")

	// 查询记录
	var optimizeAccountLogArray *model.OptimizeAccountLogArray = &model.OptimizeAccountLogArray{}
	o.db.Raw("select * from optimize_account_log t "+
		"where t.date_ between to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd') and t.account_name=? "+
		"order by t.date_ asc, t.account_name asc", beginDate, endDate, accountName).Scan(optimizeAccountLogArray)

	return *optimizeAccountLogArray
}

// 根据时间和account_name查询记录
func (o *OptimizeAccountLogDaoImpl) FindByDateAndAccountName(date string, accountName string) model.OptimizeAccountLog {
	//io.Infoln("根据时间和account_name查询记录")

	// 查询记录
	var optimizeAccountLog *model.OptimizeAccountLog = &model.OptimizeAccountLog{}
	o.db.Raw("select * from optimize_account_log t "+
		"where t.date_ = to_date(?,'yyyy-mm-dd') and t.account_name=?", date, accountName).Scan(optimizeAccountLog)

	return *optimizeAccountLog
}

// 计算最大回撤
func (o *OptimizeAccountLogDaoImpl) CalculateMaximumDrawdown() domain.MaximumDrawdownArray {
	io.Infoln("计算最大回撤")

	var maximumDrwadownArray domain.MaximumDrawdownArray
	o.db.Raw("select * from (" +
		"select (t1.total_assets-t2.total_assets)/t1.total_assets*100 MAXIMUM_DRAWDOWN, " +
		"t1.date_ DATE1, t2.date_ DATE2, t1.account_name ACCOUNT_NAME, t1.total_assets TOTAL_ASSETS1, " +
		"t2.total_assets TOTAL_ASSETS2 " +
		"from optimize_account_log t1, optimize_account_log t2 " +
		"where t1.account_name=t2.account_name and t1.date_<t2.date_ " +
		"order by (t1.total_assets-t2.total_assets)/t1.total_assets*100 desc) " +
		"where rownum<=10").Scan(&maximumDrwadownArray)

	return maximumDrwadownArray
}

// 根据日期删除记录
func (o *OptimizeAccountLogDaoImpl) DeleteByDate(date string) {
	io.Infoln("根据日期[%s]删除记录", date)

	o.db.Exec("delete from optimize_account_log t where t.date_=to_date(?,'yyyy-mm-dd')", date)
}

// 根据账号，查询当年的第一个交易日和前一年的第一个交易日
func (o *OptimizeAccountLogDaoImpl) FindCurrentYearFirstDateAndPreviousYearFirstDateByAccountName(accountName string) domain.CurrentYearFirstDateAndPreviousYearFirstDateArray {
	var currentYearFirstDateAndPreviousYearFirstDateArray domain.CurrentYearFirstDateAndPreviousYearFirstDateArray
	o.db.Raw("SELECT to_char(base_data.date_, 'yyyy-mm-dd') CURRENT_YEAR_FIRST_DATE, to_char(LAG(base_data.date_) OVER(ORDER BY base_data.date_), 'yyyy-mm-dd') PREVIOUS_YEAR_FIRST_DATE "+
		"FROM (select * from optimize_account_log oal "+
		"join (select * from ("+
		"select min(t.date_) first_date from optimize_account_log t group by TO_CHAR(t.date_, 'YYYY') "+
		"union "+
		"select max(t1.date_) first_date from optimize_account_log t1) t2 order by t2.first_date asc) o on oal.date_=o.first_date where oal.account_name=?) base_data "+
		"ORDER BY base_data.date_", accountName).Scan(&currentYearFirstDateAndPreviousYearFirstDateArray)
	return currentYearFirstDateAndPreviousYearFirstDateArray
}
